/*
* Copyright (c) 2014 Qunar.com. All Rights Reserved.
*/
package net.oschina.bilbodai.common.beanutil.property.asm.lexer.code;

import net.oschina.bilbodai.common.beanutil.util.StringUtil;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import java.lang.reflect.Method;

import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;

/**
 * @author qt-maodai  Date: 15-8-26 Time: 下午2:45
 * @version $Id$
 */
public class POJOBeanCodeApplier extends BaseCodeApplier {

    public POJOBeanCodeApplier(CodeApply codeApply) {
        super(codeApply);
    }

    @Override
    protected CodeApplyResult applyCode(MethodVisitor mv, CodeApply codeApply) throws Exception {
        String getMethodName = "get" + StringUtil.capitalize(codeApply.getThisPropertyName());
        Method propertyGetMethod = codeApply.getParentType().getMethod(getMethodName);
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(codeApply.getParentType()), getMethodName, Type.getMethodDescriptor(propertyGetMethod), false);
        return new CodeApplyResult(propertyGetMethod.getReturnType(), codeApply.getThisGenericTypes());
    }

}